import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

import { ApiService } from './api.service';

import { User } from '../domain/user';
import {LeaveRequest} from '../domain/leave-request';
import { OutRequest } from '../domain/out-request';
import {DepartmentManagerDeal} from '../domain/department-manager-deal';
import {DeputyManagerDeal} from '../domain/deputy-manager-deal';
import {MainManagerDeal} from '../domain/main-manager-deal';
import {DepartmentManagerDealPost} from '../domain/department-manager-deal-post';
import {DeputyManagerDealPost} from '../domain/deputy-manager-deal-post';
import {MainManagerDealPost} from '../domain/main-manager-deal-post';
/*

@Author:李朝晖

@Date: 2018-12-12

@经理的服务

@Last Modified time: 2018-12-16
*/
@Injectable({
  providedIn: 'root'
})
export class ManagerService {

  private api_url;
  private headers;

  constructor(private apiService: ApiService,
    private http: HttpClient) {
    this.api_url = apiService.getUrl();
    this.headers = apiService.getHeaders();
  }

  getUser(id: number): Observable<User[]> {
    return this.http.get<User[]>(this.api_url + '/users?id=' + id)
      .pipe(
        catchError(this.handleError('getUser', []))
      );
  }
  
  getUsers(): Observable<User[]> {
    return this.http.get<User[]>(this.api_url + '/users')
      .pipe(
        catchError(this.handleError('getUsers', []))
      );
  }

  getAllLeaveRequest(): Observable<LeaveRequest[]> {
    return this.http.get<LeaveRequest[]>(this.api_url + '/leave_requests')
      .pipe(
        catchError(this.handleError('getAllLeaveRequest', []))
      );
  }

  getAllOutRequest(): Observable<OutRequest[]> {
    return this.http.get<OutRequest[]>(this.api_url + '/out_requests')
      .pipe(
        catchError(this.handleError('getAllOutRequest', []))
      );
  }
  getDepartmentLeaveRequest(department:string): Observable<LeaveRequest[]> {
    return this.http.get<LeaveRequest[]>(this.api_url + '/leave_requests?department=' + department)
      .pipe(
        catchError(this.handleError('getAllLeaveRequest', []))
      );
  }

  getDepartmentOutRequest(department:string): Observable<OutRequest[]> {
    return this.http.get<OutRequest[]>(this.api_url + '/out_requests?department=' + department)
      .pipe(
        catchError(this.handleError('getAllOutRequest', []))
      );
  }

  getDepartmentManagerDeal(id: number): Observable<DepartmentManagerDeal[]> {
    return this.http.get<DepartmentManagerDeal[]>(this.api_url + '/department_manager_deals?department_manager_id=' + id)
      .pipe(
        catchError(this.handleError('getDepartmentManagerDeal', []))
      );
  }

  getDeputyManagerDeal(id: number): Observable<DeputyManagerDeal[]> {
    return this.http.get<DeputyManagerDeal[]>(this.api_url + '/deputy_manager_deals?deputy_manager_id=' + id)
      .pipe(
        catchError(this.handleError('getDeputyManagerDeal', []))
      );
  }

  getMainManagerDeal(id: number): Observable<MainManagerDeal[]> {
    return this.http.get<MainManagerDeal[]>(this.api_url + '/main_manager_deals?main_manager_id=' + id)
      .pipe(
        catchError(this.handleError('getMainManagerDeal', []))
      );
  }

  updateLeaveRequest(leave: LeaveRequest):Observable<any> {
    return this.http.put(this.api_url + "/leave_requests/" + leave.id, leave, this.headers).pipe(
      catchError(this.handleError<any>('updateLeaveRequest'))
    );
  }

  updateOutRequest(out: OutRequest):Observable<any> {
    return this.http.put(this.api_url + "/out_requests/" + out.id, out, this.headers).pipe(
      catchError(this.handleError<any>('updateLeaveRequest'))
    );
  }

  //部门经理新建处理
  createDepartmentManagerDeal(deal:DepartmentManagerDealPost): Observable<any> {
    return this.http.post(this.api_url + "/department_manager_deals", deal, this.headers).pipe(
      catchError(this.handleError<any>('createDepartmentDeal'))
    );
  }
  //副总经理新建处理
  createDeputyManagerDeal(deal:DeputyManagerDealPost): Observable<any> {
    return this.http.post(this.api_url + "/deputy_manager_deals", deal, this.headers).pipe(
      catchError(this.handleError<any>('createDuputManagerDeal'))
    );
  }
  //总经理新建处理
  createMainManagerDeal(deal:MainManagerDealPost): Observable<any> {
    return this.http.post(this.api_url + "/main_manager_deals", deal, this.headers).pipe(
      catchError(this.handleError<any>('createMainManagerDeal'))
    );
  }

  getLeaveRequest(id: number): Observable<LeaveRequest[]> {
    return this.http.get<LeaveRequest[]>(this.api_url + '/leave_requests?id=' + id)
      .pipe(
        catchError(this.handleError('getLeaveRequest', []))
      );
  }

  getOutRequest(id: number): Observable<OutRequest[]> {
    return this.http.get<OutRequest[]>(this.api_url + '/out_requests?id=' + id)
      .pipe(
        catchError(this.handleError('getOutRequest', []))
      );
  }

  //更新用户年假
  updateUserleave(user: User):Observable<any> {
    return this.http.put(this.api_url + "/users/" + user.id, user, this.headers).pipe(
      catchError(this.handleError<any>('updateLeaveRequest'))
    );
  }
  
  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      console.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}
